﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class UseToolbarDynamic : MonoBehaviour
{

    public RectTransform toolbarPanel;

    IEnumerator Start()
    {
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
        string path = string.Format("file:///{0}/{1}", Application.dataPath, "AssetBundle/OSX/dynamictoolbar");
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        string path = string.Format("{0}/{1}", Application.dataPath, "AssetBundle/Windows/dynamictoolbar");
#endif
        var uwr = UnityWebRequestAssetBundle.GetAssetBundle(path);
        yield return uwr.SendWebRequest();        

        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(uwr);
        string[] assets = ab.GetAllAssetNames();
        foreach (string a in assets)
        {
            Debug.Log(a);
        }

        //setp 1. create eventcomponentobject
        GameObject eventComponentPrefab = ab.LoadAsset<GameObject>("assets/script/toolbar/eventcomponentobject.prefab");
        if (null == eventComponentPrefab)
        {
            Debug.LogError("null eventcomponentobject.prefab");
            yield break;
        }
        //Instantiate(eventComponentPrefab);


        //step 2. create toolbar
        ToolbarAsset toolbarAsset = ab.LoadAsset<ToolbarAsset>("assets/script/toolbar/toolbar.asset");
        if (null == toolbarAsset)
        {
            Debug.LogError("null toolbar.asset");
            yield break;
        }

        Button prefab = null;
        ToolbarButtonObject item = null;
        int count = toolbarAsset.toolbarButtons.Count;
        for (int i = 0; i < count; i++)
        {
            item = toolbarAsset.toolbarButtons[i];
            Debug.LogFormat("Create Button {0}", item.name);

            switch (item.type)
            {
                case ToolbarButtonObject.IconType.Brands:
                    prefab = toolbarAsset.BrandsButtonPrefab;
                    break;
                case ToolbarButtonObject.IconType.Regular:
                    prefab = toolbarAsset.RegularButtonPrefab;
                    break;
                case ToolbarButtonObject.IconType.Solid:
                    prefab = toolbarAsset.SolidButtonPrefab;
                    break;
            }

            CreateButton(item, prefab);
        }
    }

    private void CreateButton(ToolbarButtonObject item, Button prefab)
    {
        Button button = Instantiate(prefab);
        //item.icon;
        //item.iconSize;
        //item.color;

        Text icon = button.transform.Find("Text").GetComponent<Text>();
        if (null == icon)
        {
            return;
        }

        RectTransform rtButton = button.GetComponent<RectTransform>();
        rtButton.SetParent(toolbarPanel);

        icon.text = item.icon;
        if (item.iconSize > 0)
        {
            icon.fontSize = item.iconSize;
        }

        icon.color = item.color;

        button.gameObject.name = string.Format("Button_{0}", item.name);

        //event
        button.onClick.AddListener( () => { item.onClick.Invoke(button); } );
    }
}
